package com.huadao.cart_ccs.common.dialog;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.TimePicker;

import com.huadao.cart_ccs.R;
import com.huadao.cart_ccs.common.ParamsPreferences;
import com.huadao.cart_ccs.common.TimeUtil;
import com.huadao.cart_ccs.common.dialog.effects.RockButtonAttr;
import com.huadao.cart_ccs.common.inputControl.MaxDecimalTextWatch;
import com.huadao.cart_ccs.common.inputControl.MaxNumberTextWatch;
import com.huadao.cart_ccs.common.inputControl.NumberMaxAttention;
import com.huadao.cart_ccs.data.bo.PeristalticPumpBO;
import com.huadao.cart_ccs.data.task.ChangeLiquidTask;
import com.huadao.cart_ccs.data.task.CleanPipesTask;
import com.huadao.cart_ccs.experiment.paramsSetting.ParamConsts;
import com.huadao.cart_ccs.main.CartApplication;

import java.util.Calendar;
import java.util.Date;

public class DialogFragmentHelper {

    private static final String DIALOG_POSITIVE = "确定";
    private static final String DIALOG_NEGATIVE = "取消";

    private static final String TAG_HEAD = DialogFragmentHelper.class.getSimpleName();

    /**
     * 加载中的弹出窗
     */
    private static final int PROGRESS_THEME = R.style.Base_AlertDialog;
    private static final String PROGRESS_TAG = TAG_HEAD + ":progress";

    public static CommonDialogFragment showProgress(FragmentManager fragmentManager, String message) {
        return showProgress(fragmentManager, message, true, null);
    }

    public static CommonDialogFragment showProgress(FragmentManager fragmentManager, String message, boolean cancelable) {
        return showProgress(fragmentManager, message, cancelable, null);
    }

    public static CommonDialogFragment showProgress(FragmentManager fragmentManager, final String message, boolean cancelable
            , CommonDialogFragment.OnDialogCancelListener cancelListener) {

        CommonDialogFragment dialogFragment = CommonDialogFragment.newInstance(new CommonDialogFragment.OnCallDialog() {
            @Override
            public Dialog getDialog(Context context) {
                ProgressDialog progressDialog = new ProgressDialog(context, PROGRESS_THEME);
                progressDialog.setMessage(message);
                return progressDialog;
            }
        }, cancelable, cancelListener);
        dialogFragment.show(fragmentManager, PROGRESS_TAG);
        return dialogFragment;
    }

    /**
     * 简单提示弹出窗
     */
    private static final int TIPS_THEME = R.style.Base_AlertDialog;
    private static final String TIPS_TAG = TAG_HEAD + ":tips";

    public static void showTips(FragmentManager fragmentManager, String message) {
        showTips(fragmentManager, message, true, null);
    }

    public static void showTips(FragmentManager fragmentManager, String message, boolean cancelable) {
        showTips(fragmentManager, message, cancelable, null);
    }

    public static void showTips(FragmentManager fragmentManager, final String message, boolean cancelable
            , CommonDialogFragment.OnDialogCancelListener cancelListener) {

        CommonDialogFragment dialogFragment = CommonDialogFragment.newInstance(new CommonDialogFragment.OnCallDialog() {
            @Override
            public Dialog getDialog(Context context) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context, TIPS_THEME);
                builder.setMessage(message);
                return builder.create();
            }
        }, cancelable, cancelListener);
        dialogFragment.show(fragmentManager, TIPS_TAG);
    }

    /**
     * 选择日期
     */
    public static DialogFragment showDateDialog(FragmentManager fragmentManager, final String title, final Calendar calendar
            , final IDialogResultListener<Calendar> resultListener, final boolean cancelable) {
        CommonDialogFragment dialogFragment = CommonDialogFragment.newInstance(new CommonDialogFragment.OnCallDialog() {
            @Override
            public Dialog getDialog(Context context) {
                final DatePickerDialog datePickerDialog = new DatePickerDialog(context, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        calendar.set(year, month, dayOfMonth);
                        resultListener.onDataResult(calendar);
                    }
                }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));

                datePickerDialog.setTitle(title);
                datePickerDialog.setOnShowListener(new DialogInterface.OnShowListener() {
                    @Override
                    public void onShow(DialogInterface dialog) {
                        datePickerDialog.getButton(DialogInterface.BUTTON_POSITIVE).setText(DIALOG_POSITIVE);
                        datePickerDialog.getButton(DialogInterface.BUTTON_NEGATIVE).setText(DIALOG_NEGATIVE);
                    }
                });
                return datePickerDialog;

            }
        }, cancelable, null);
        dialogFragment.show(fragmentManager, null);
        return null;
    }


    /**
     * 选择时间
     */
    public static void showTimeDialog(FragmentManager manager, final String title, final Calendar calendar, final IDialogResultListener<Calendar> resultListener, final boolean cancelable) {
        CommonDialogFragment dialogFragment = CommonDialogFragment.newInstance(new CommonDialogFragment.OnCallDialog() {
            @Override
            public Dialog getDialog(Context context) {
                final TimePickerDialog dateDialog = new TimePickerDialog(context, R.style.Base_AlertDialog, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        if (resultListener != null) {
                            calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                            calendar.set(Calendar.MINUTE, minute);
                            resultListener.onDataResult(calendar);
                        }
                    }
                }, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true);

                dateDialog.setTitle(title);
                dateDialog.setOnShowListener(new DialogInterface.OnShowListener() {
                    @Override
                    public void onShow(DialogInterface dialog) {
                        dateDialog.getButton(DialogInterface.BUTTON_POSITIVE).setText(DIALOG_POSITIVE);
                        dateDialog.getButton(DialogInterface.BUTTON_NEGATIVE).setText(DIALOG_NEGATIVE);
                    }
                });

                return dateDialog;
            }
        }, cancelable, null);
        dialogFragment.show(manager, null);
    }

    public static void showMessageDialog(Context context, String title, String message) {
        RockDialog.RockDialogBuilder dialogBuilder = new RockDialog.RockDialogBuilder(context);
        dialogBuilder.withTitle(title)
                .withMessage(message)
                .addButton(context.getString(R.string.positive), new RockCallBack() {
                    @Override
                    public void onClick(RockDialog rockDialog, View view) {
                        rockDialog.dismiss();
                    }
                })
                .create().show();
    }

    public static void showMultiLineMessageDialog(Context context, String title, String message) {
        TextView customView = (TextView) LayoutInflater.from(context).inflate(R.layout.dialog_multi_line_message, null);
        customView.setText(message);
        RockDialog.RockDialogBuilder dialogBuilder = new RockDialog.RockDialogBuilder(context);
        dialogBuilder.withTitle(title)
                .withCustomView(customView)
                .addButton(context.getString(R.string.positive), new RockCallBack() {
                    @Override
                    public void onClick(RockDialog rockDialog, View view) {
                        rockDialog.dismiss();
                    }
                })
                .create().show();
    }

    public static void showConfirmDialog(Context context, String title, String message, IDialogResultListener<Integer> listener, boolean cancelable) {
        showTwoOperateDialog(context, title, message, R.string.positive, R.string.negative, listener, cancelable);
    }

    public static void showInsertDialog(FragmentManager manager, final String title, final int inputType, final IDialogResultListener<String> resultListener, final boolean cancelable) {
        CommonDialogFragment dialogFragment = CommonDialogFragment.newInstance(new CommonDialogFragment.OnCallDialog() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public Dialog getDialog(Context context) {
                final EditText editText = new EditText(context);
                editText.setBackground(null);
                editText.setInputType(inputType);
                editText.setPadding(60, 40, 0, 0);
                AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.Base_AlertDialog);
                builder.setTitle(title);
                builder.setView(editText);
                builder.setPositiveButton(DIALOG_POSITIVE, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (resultListener != null) {
                            resultListener.onDataResult(editText.getText().toString());
                        }
                    }
                });
                builder.setNegativeButton(DIALOG_NEGATIVE, null);
                return builder.create();

            }
        }, cancelable, null);
        dialogFragment.show(manager, null);

    }

    public static void showCleanPipesDialog(Context context, CleanPipesTask cleanPipesTask, final IDialogResultListener<Object[]> listener) {
        View customView = LayoutInflater.from(context).inflate(R.layout.dialog_clean_pipes, null);
        final EditText et_fluid_amount = customView.findViewById(R.id.et_fluid_amount);
        final EditText et_fluid_pump = customView.findViewById(R.id.et_fluid_pump);
        final EditText et_loop_times = customView.findViewById(R.id.et_loop_times);
        final EditText et_circling_pump = customView.findViewById(R.id.et_circling_pump);
        final EditText et_waste_pump = customView.findViewById(R.id.et_waste_pump);
        final EditText et_clean_numbers = customView.findViewById(R.id.et_clean_numbers);
        et_fluid_pump.setText(String.valueOf(ParamsPreferences.getInstance().getFluidIntakePumpSpeed()));
        et_waste_pump.setText(String.valueOf(ParamsPreferences.getInstance().getWastePumpSpeed()));
        et_circling_pump.setText(String.valueOf(ParamsPreferences.getInstance().getCirculatingPumpSpeed()));
        if (cleanPipesTask != null) {
            et_fluid_amount.setText(String.valueOf(cleanPipesTask.getFluidAmount()));
            et_fluid_pump.setText(String.valueOf(cleanPipesTask.getFluidSpeed()));
            et_loop_times.setText(String.valueOf(cleanPipesTask.getLoopTime() / 60));
            et_circling_pump.setText(String.valueOf(cleanPipesTask.getCirclingSpeed()));
            et_waste_pump.setText(String.valueOf(cleanPipesTask.getWasteSpeed()));
            et_clean_numbers.setText(String.valueOf(String.valueOf(cleanPipesTask.getCleanNumbers())));
            et_fluid_amount.setEnabled(false);
            et_fluid_pump.setEnabled(false);
            et_loop_times.setEnabled(false);
            et_circling_pump.setEnabled(false);
            et_waste_pump.setEnabled(false);
            et_clean_numbers.setEnabled(false);
        }

        RockDialog.RockDialogBuilder dialogBuilder = new RockDialog.RockDialogBuilder(context);
        if (cleanPipesTask == null) {
            dialogBuilder.addButton(context.getString(R.string.run), RockButtonAttr.BUTTON_POSITIVE, new RockCallBack() {
                @Override
                public void onClick(RockDialog rockDialog, View view) {
                    if (listener != null) {
                        String amount = et_fluid_amount.getText().toString();
                        String fluidSpeed = et_fluid_pump.getText().toString();
                        String loopTime = et_loop_times.getText().toString();
                        String circlingSpeed = et_circling_pump.getText().toString();
                        String wasteSpeed = et_waste_pump.getText().toString();
                        String cleanNumbers = et_clean_numbers.getText().toString();
                        listener.onDataResult(new Object[]{DialogInterface.BUTTON_POSITIVE, amount, fluidSpeed, loopTime, circlingSpeed, wasteSpeed, cleanNumbers});
                    }
                    rockDialog.dismiss();
                }
            });
        } else {
            dialogBuilder.addButton("清洗中...", RockButtonAttr.BUTTON_INFO, null);
        }
        dialogBuilder.withTitle(context.getString(R.string.cleanPipes))
                .withCustomView(customView)
                .isCancelableOnTouchOutside(false)
                .create().show();
    }

    public static void showPumpOperateDialog(final Context context, final String title, final int defaultSpeed, final byte defaultDirection, final IDialogResultListener<Object[]> listener, NumberMaxAttention numberMaxAttention) {
        View customView = LayoutInflater.from(context).inflate(R.layout.dialog_pump_operate, null);
        final EditText editText = customView.findViewById(R.id.editText);
        final RadioGroup rg_direction = customView.findViewById(R.id.rg_direction);
        TextView textView = customView.findViewById(R.id.textView);
        TextView tv_unit = customView.findViewById(R.id.unit);
        textView.setText(R.string.speed);
        tv_unit.setText(R.string.speed_unit);
        String speedStr = String.valueOf(defaultSpeed);
        editText.setText(speedStr);
        editText.setSelection(speedStr.length());
        editText.setInputType(InputType.TYPE_CLASS_NUMBER);
        editText.addTextChangedListener(new MaxNumberTextWatch(ParamConsts.maxPumpSpeed, editText, false, numberMaxAttention));
        if (defaultDirection == PeristalticPumpBO.directionReverse) {
            ((RadioButton) rg_direction.getChildAt(1)).setChecked(true);
        } else {
            ((RadioButton) rg_direction.getChildAt(0)).setChecked(true);
        }

        RockDialog.RockDialogBuilder dialogBuilder = new RockDialog.RockDialogBuilder(context);
        dialogBuilder.withTitle(title)
                .addButton(context.getString(R.string.stop), RockButtonAttr.BUTTON_NEGATIVE, new RockCallBack() {
                    @Override
                    public void onClick(RockDialog rockDialog, View view) {
                        if (listener != null) {
                            listener.onDataResult(new Object[]{DialogInterface.BUTTON_NEGATIVE});
                        }
                        rockDialog.dismiss();
                    }
                })
                .addButton(context.getString(R.string.run), RockButtonAttr.BUTTON_POSITIVE, new RockCallBack() {
                    @Override
                    public void onClick(RockDialog rockDialog, View view) {
                        if (listener != null) {
                            String speed = editText.getText().toString();
                            int checkedRadioButtonId = rg_direction.getCheckedRadioButtonId();
                            byte direction;
                            if (checkedRadioButtonId == R.id.rb_directionReverse) {
                                direction = PeristalticPumpBO.directionReverse;
                            } else {
                                direction = PeristalticPumpBO.directionForward;
                            }
                            listener.onDataResult(new Object[]{DialogInterface.BUTTON_POSITIVE, speed, direction});
                        }
                        rockDialog.dismiss();
                    }
                })
                .withCustomView(customView)
                .create().show();
    }

    public static void showTwoOperateDialog(Context context, String title, String message, int posRes, int negRes, final IDialogResultListener<Integer> listener, boolean cancelable) {
        RockDialog.RockDialogBuilder dialogBuilder = new RockDialog.RockDialogBuilder(context);
        dialogBuilder.withTitle(title)
                .withMessage(message)
                .addButton(context.getString(negRes), RockButtonAttr.BUTTON_NEGATIVE, new RockCallBack() {
                    @Override
                    public void onClick(RockDialog rockDialog, View view) {
                        if (listener != null) {
                            listener.onDataResult(DialogInterface.BUTTON_NEGATIVE);
                        }
                        rockDialog.dismiss();
                    }
                })
                .addButton(context.getString(posRes), RockButtonAttr.BUTTON_POSITIVE, new RockCallBack() {
                    @Override
                    public void onClick(RockDialog rockDialog, View view) {
                        if (listener != null) {
                            listener.onDataResult(DialogInterface.BUTTON_POSITIVE);
                        }
                        rockDialog.dismiss();
                    }
                })
                .isCancelableOnTouchOutside(cancelable)
                .create().show();
    }

    public static void showThreeOperateDialog(final Context context, String title, String message, int posRes, int negRes, int neuRes, final IDialogResultListener<Integer> listener) {
        RockDialog.RockDialogBuilder dialogBuilder = new RockDialog.RockDialogBuilder(context);
        dialogBuilder.withTitle(title)
                .withMessage(message)
                .addButton(context.getString(posRes), new RockCallBack() {
                    @Override
                    public void onClick(RockDialog rockDialog, View view) {
                        if (listener != null) {
                            listener.onDataResult(DialogInterface.BUTTON_POSITIVE);
                        }
                        rockDialog.dismiss();
                    }
                })
                .addButton(context.getString(negRes), new RockCallBack() {
                    @Override
                    public void onClick(RockDialog rockDialog, View view) {
                        if (listener != null) {
                            listener.onDataResult(DialogInterface.BUTTON_NEGATIVE);
                        }
                        rockDialog.dismiss();
                    }
                })
                .addButton(context.getString(neuRes), new RockCallBack() {
                    @Override
                    public void onClick(RockDialog rockDialog, View view) {
                        if (listener != null) {
                            listener.onDataResult(DialogInterface.BUTTON_NEUTRAL);
                        }
                        rockDialog.dismiss();
                    }
                }).create().show();
    }


    public static void showMultipleOperateDialog(Context context, String title, String message, final String[] operateList, final IDialogResultListener<Integer> listener, boolean isButtonList, boolean cancelable) {
        RockDialog.RockDialogBuilder dialogBuilder = new RockDialog.RockDialogBuilder(context);
        for (int i = 0; i < operateList.length; i++) {
            final int position = i;
            dialogBuilder.addButton(operateList[i], new RockCallBack() {
                @Override
                public void onClick(RockDialog rockDialog, View view) {
                    listener.onDataResult(position);
                    rockDialog.dismiss();
                }
            });
        }
        dialogBuilder.withTitle(title)
                .withMessage(message)
                .withButtonList(isButtonList)
                .isCancelableOnTouchOutside(cancelable).create().show();
    }

//    public static void doorOperateDialog(Context context,IDialogResultListener<Integer> listener){
//        String[] door_items = new String[]{context.getString(R.string.open_door), context.getString(R.string.close_door), context.getString(R.string.read), context.getString(R.string.stop)};
//        DialogFragmentHelper.showMultipleOperateDialog(context, context.getString(R.string.door), null, door_items, listener, true, true);
//    }

    public static void showNormalInputDialog(Context context, String title, String placeHolder, String defaultText, final IDialogResultListener<Object[]> listener) {
        View customView = LayoutInflater.from(context).inflate(R.layout.dialog_edittext, null);
        final EditText editText = customView.findViewById(R.id.editText);
        editText.setHint(placeHolder);
        if (defaultText != null) {
            editText.setText(defaultText);
            editText.setSelection(defaultText.length());
        }

        RockDialog.RockDialogBuilder dialogBuilder = new RockDialog.RockDialogBuilder(context);
        dialogBuilder.withTitle(title)
                .addButton(context.getString(R.string.negative), RockButtonAttr.BUTTON_NEGATIVE, new RockCallBack() {
                    @Override
                    public void onClick(RockDialog rockDialog, View view) {
                        if (listener != null) {
                            listener.onDataResult(new Object[]{DialogInterface.BUTTON_NEGATIVE});
                        }
                        rockDialog.dismiss();
                    }
                })
                .addButton(context.getString(R.string.positive), RockButtonAttr.BUTTON_POSITIVE, new RockCallBack() {
                    @Override
                    public void onClick(RockDialog rockDialog, View view) {
                        if (listener != null) {
                            Object[] res = new Object[2];
                            res[0] = DialogInterface.BUTTON_POSITIVE;
                            CharSequence text = editText.getText();
                            if (text != null && text.length() > 0) {
                                res[1] = text.toString();
                            } else {
                                res[1] = null;
                            }
                            listener.onDataResult(res);
                        }
                        rockDialog.dismiss();
                    }
                })
                .withCustomView(customView)
                .create().show();
    }

    public static void showInputDialog(Context context, String title, String paramName, String unit, String placeHolder, int inputType, String defaultInput, int max, int posRes, int negRes, final IDialogResultListener<Object[]> listener, NumberMaxAttention numberMaxAttention,boolean isCancelAble) {
        View customView = LayoutInflater.from(context).inflate(R.layout.dialog_edittext, null);
        final EditText editText = customView.findViewById(R.id.editText);
        TextView textView = customView.findViewById(R.id.textView);
        TextView tv_unit = customView.findViewById(R.id.unit);
        textView.setText(paramName);
        tv_unit.setText(unit);
        if (defaultInput != null) {
            editText.setText(defaultInput);
            editText.setSelection(defaultInput.length());
        }
        editText.setInputType(inputType);
        if (inputType == InputType.TYPE_CLASS_NUMBER) {
            editText.addTextChangedListener(new MaxNumberTextWatch(max, editText, false, numberMaxAttention));
        } else if (inputType == (InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER)) {
            editText.addTextChangedListener(new MaxDecimalTextWatch(max, 2, editText, false, numberMaxAttention));
        }
        editText.setHint(placeHolder);

        RockDialog.RockDialogBuilder dialogBuilder = new RockDialog.RockDialogBuilder(context);
        dialogBuilder.withTitle(title)
                .addButton(context.getString(negRes), RockButtonAttr.BUTTON_NEGATIVE, new RockCallBack() {
                    @Override
                    public void onClick(RockDialog rockDialog, View view) {
                        if (listener != null) {
                            listener.onDataResult(new Object[]{DialogInterface.BUTTON_NEGATIVE});
                        }
                        rockDialog.dismiss();
                    }
                })
                .addButton(context.getString(posRes), RockButtonAttr.BUTTON_POSITIVE, new RockCallBack() {
                    @Override
                    public void onClick(RockDialog rockDialog, View view) {
                        if (listener != null) {
                            Object[] res = new Object[2];
                            res[0] = DialogInterface.BUTTON_POSITIVE;
                            CharSequence text = editText.getText();
                            if (text != null && text.length() > 0) {
                                res[1] = text.toString();
                            } else {
                                res[1] = null;
                            }
                            listener.onDataResult(res);
                        }
                        rockDialog.dismiss();
                    }
                })
                .withCustomView(customView)
                .isCancelableOnTouchOutside(isCancelAble)
                .create().show();
    }

    public static void showStirrerDialog(Context context, String defaultInput, IDialogResultListener<Object[]> listener, NumberMaxAttention numberMaxAttention) {
        showInputDialog(context, context.getString(R.string.magnetic_stirrer), context.getString(R.string.speed), context.getString(R.string.speed_unit), null, InputType.TYPE_CLASS_NUMBER, defaultInput, ParamConsts.maxStirrerSpeed, R.string.run, R.string.stop, listener, numberMaxAttention,true);
    }

    public static void showCultureTankDialog(Context context, String defaultInput, IDialogResultListener<Object[]> listener, NumberMaxAttention numberMaxAttention) {
        showInputDialog(context, context.getString(R.string.cultureTankElectric), context.getString(R.string.speed), context.getString(R.string.speed_unit), null, InputType.TYPE_CLASS_NUMBER, defaultInput, ParamConsts.maxStirrerSpeed, R.string.run, R.string.stop, listener, numberMaxAttention,true);
    }

    public static void showEnvironmentDialog(Context context, String envir, String unit, String defaultInput, int max, IDialogResultListener<Object[]> listener, NumberMaxAttention numberMaxAttention) {
        showInputDialog(context, envir, envir, unit, null, InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER, defaultInput, max, R.string.setting, R.string.read, listener, numberMaxAttention,true);
    }

    //浓缩置换
    public static void showConcentrationReplacementDialog(Context context, ChangeLiquidTask changeLiquidTask, final IDialogResultListener<Object[]> listener) {
        View customView = LayoutInflater.from(context).inflate(R.layout.dialog_concentration_replacement, null);
        final EditText et_waste_amount = customView.findViewById(R.id.et_waste_amount);
        final EditText et_fluid_amount = customView.findViewById(R.id.et_fluid_amount);
        final EditText et_numbers = customView.findViewById(R.id.et_numbers);
        final EditText et_date = customView.findViewById(R.id.et_date);
        final EditText et_time = customView.findViewById(R.id.et_time);
        final EditText et_operator = customView.findViewById(R.id.et_operator);
        final String user = CartApplication.getInstance().getOperator();
        final Date date = new Date();

        if (changeLiquidTask != null) {
            et_waste_amount.setText(String.valueOf(changeLiquidTask.getChangeLiquidBO().getChangeLiquidTurnList().get(0).getAmountList().get(0).getFluid()));
            et_fluid_amount.setText(String.valueOf(changeLiquidTask.getChangeLiquidBO().getChangeLiquidTurnList().get(0).getAmountList().get(0).getWaste()));
            et_numbers.setText(String.valueOf(changeLiquidTask.getChangeLiquidBO().getChangeLiquidTurnList().get(0).getAmountList().size()));
            et_date.setText(TimeUtil.date_sdf.format(changeLiquidTask.getOperateDate()));
            et_time.setText(TimeUtil.time_sdf.format(changeLiquidTask.getOperateDate()));
            et_operator.setText(changeLiquidTask.getOperator());
            et_waste_amount.setEnabled(false);
            et_fluid_amount.setEnabled(false);
            et_numbers.setEnabled(false);
            et_date.setEnabled(false);
            et_time.setEnabled(false);
            et_operator.setEnabled(false);
        } else {
            et_operator.setText(user);
            et_date.setText(TimeUtil.date_sdf.format(date));
            et_time.setText(TimeUtil.time_sdf.format(date));
        }

        RockDialog.RockDialogBuilder dialogBuilder = new RockDialog.RockDialogBuilder(context);
        dialogBuilder.withTitle("浓缩置换")
                .addButton(context.getString(R.string.negative), RockButtonAttr.BUTTON_NEGATIVE, new RockCallBack() {
                    @Override
                    public void onClick(RockDialog rockDialog, View view) {
                        if (listener != null) {
                            listener.onDataResult(new Object[]{DialogInterface.BUTTON_NEGATIVE});
                        }
                        rockDialog.dismiss();
                    }
                })
                .addButton(context.getString(R.string.positive), RockButtonAttr.BUTTON_POSITIVE, new RockCallBack() {
                    @Override
                    public void onClick(RockDialog rockDialog, View view) {
                        if (listener != null) {
                            Object[] res = new Object[6];
                            res[0] = DialogInterface.BUTTON_POSITIVE;
                            res[1] = et_waste_amount.getText().toString();
                            res[2] = et_fluid_amount.getText().toString();
                            res[3] = et_numbers.getText().toString();
                            res[4] = date;
                            res[5] = user;
                            listener.onDataResult(res);
                        }
                        rockDialog.dismiss();
                    }
                })
                .withCustomView(customView)
                .isCancelableOnTouchOutside(false)
                .create().show();
    }

    public static void showMaterialKillDialog(Context context, CleanPipesTask cleanPipesTask, final IDialogResultListener<Object[]> listener) {
        View customView = LayoutInflater.from(context).inflate(R.layout.dialog_material_kill, null);
        final EditText et_fluid_amount = customView.findViewById(R.id.et_fluid_amount);
        final EditText et_waste_maount = customView.findViewById(R.id.et_waste_amount);
        final EditText et_loop_times = customView.findViewById(R.id.et_loop_times);
        final EditText et_date = customView.findViewById(R.id.et_operate_date);
        final EditText et_time = customView.findViewById(R.id.et_operate_time);
        final EditText et_operator = customView.findViewById(R.id.et_operator);
        final String user = CartApplication.getInstance().getOperator();
        if (cleanPipesTask != null) {
            et_fluid_amount.setText(String.valueOf(cleanPipesTask.getFluidAmount()));
            et_waste_maount.setText(String.valueOf(cleanPipesTask.getWasteAmount()));
            et_loop_times.setText(String.valueOf(cleanPipesTask.getLoopTime()));
            if (cleanPipesTask.getOperateDate() != null) {
                et_date.setText(TimeUtil.date_sdf.format(cleanPipesTask.getOperateDate()));
                et_time.setText(TimeUtil.time_sdf.format(cleanPipesTask.getOperateDate()));
            }
            et_operator.setText(cleanPipesTask.getOperator());
            et_fluid_amount.setEnabled(false);
            et_waste_maount.setEnabled(false);
            et_loop_times.setEnabled(false);
            et_date.setEnabled(false);
            et_time.setEnabled(false);
            et_operator.setEnabled(false);
        }else {
            et_operator.setText(user);
            final Date date = new Date();
            et_date.setText(TimeUtil.date_sdf.format(date));
            et_time.setText(TimeUtil.time_sdf.format(date));
        }
        RockDialog.RockDialogBuilder dialogBuilder = new RockDialog.RockDialogBuilder(context);
        if(cleanPipesTask==null){
            dialogBuilder.withTitle("耗材消毒")
                    .addButton(context.getString(R.string.run), RockButtonAttr.BUTTON_POSITIVE, new RockCallBack() {
                        @Override
                        public void onClick(RockDialog rockDialog, View view) {
                            if (listener != null) {
                                String amount = et_fluid_amount.getText().toString();
                                String fluidSpeed = et_waste_maount.getText().toString();
                                String loopTime = et_loop_times.getText().toString();
                                listener.onDataResult(new Object[]{DialogInterface.BUTTON_POSITIVE, amount, fluidSpeed, loopTime});
                            }
                            rockDialog.dismiss();
                        }
                    });
        }else {
            dialogBuilder.withTitle("耗材消毒")
                    .addButton("耗材消毒中...", RockButtonAttr.BUTTON_INFO, null);
        }
        dialogBuilder.withCustomView(customView)
                .isCancelableOnTouchOutside(false)
                .create().show();
    }

    public static void showCleanConfirm(Context context, final IDialogResultListener<Object[]> listener) {
        View customView = LayoutInflater.from(context).inflate(R.layout.dialog_clean_confirm, null);
        final CheckBox cb_clean_confirmed = customView.findViewById(R.id.cb_clean_confirmed);
        final EditText et_date = customView.findViewById(R.id.et_operate_date);
        final EditText et_time = customView.findViewById(R.id.et_operate_time);
        final EditText et_operator = customView.findViewById(R.id.et_operator);
        final String user = CartApplication.getInstance().getOperator();
        et_operator.setText(user);
        final Date date = new Date();
        et_date.setText(TimeUtil.date_sdf.format(date));
        et_time.setText(TimeUtil.time_sdf.format(date));
        RockDialog.RockDialogBuilder dialogBuilder = new RockDialog.RockDialogBuilder(context);
        dialogBuilder.withTitle("耗材消毒")
                .addButton(context.getString(R.string.run), RockButtonAttr.BUTTON_POSITIVE, new RockCallBack() {
                    @Override
                    public void onClick(RockDialog rockDialog, View view) {
                        if (listener != null) {
                            Boolean isChecked = cb_clean_confirmed.isChecked();
                            listener.onDataResult(new Object[]{DialogInterface.BUTTON_POSITIVE, isChecked});
                        }
                        rockDialog.dismiss();
                    }
                })
                .withCustomView(customView)
                .isCancelableOnTouchOutside(false)
                .create().show();
    }

    public static void showLiquidOperateDialog(Context context, String title, String paramName, String unit, String placeHolder, int inputType, String defaultInput, int max, int posRes, int negRes, final IDialogResultListener<Object[]> listener, NumberMaxAttention numberMaxAttention) {
        View customView = LayoutInflater.from(context).inflate(R.layout.dialog_liquid_operate, null);
        final EditText editText = customView.findViewById(R.id.editText);
        TextView textView = customView.findViewById(R.id.textView);
        TextView tv_unit = customView.findViewById(R.id.unit);
        textView.setText(paramName);
        tv_unit.setText(unit);
        TextView tv_date = customView.findViewById(R.id.tv_date);
        TextView tv_time = customView.findViewById(R.id.tv_time);
        final TextView tv_operator = customView.findViewById(R.id.tv_operator);
        final Date date = new Date();
        tv_date.setText(TimeUtil.date_sdf.format(date));
        tv_time.setText(TimeUtil.time_sdf.format(date));
        tv_operator.setText(CartApplication.getInstance().getOperator());

        if (defaultInput != null) {
            editText.setText(defaultInput);
            editText.setSelection(defaultInput.length());
        }
        editText.setInputType(inputType);
        if (inputType == InputType.TYPE_CLASS_NUMBER) {
            editText.addTextChangedListener(new MaxNumberTextWatch(max, editText, false, numberMaxAttention));
        } else if (inputType == (InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER)) {
            editText.addTextChangedListener(new MaxDecimalTextWatch(max, 2, editText, false, numberMaxAttention));
        }
        editText.setHint(placeHolder);

        RockDialog.RockDialogBuilder dialogBuilder = new RockDialog.RockDialogBuilder(context);
        dialogBuilder.withTitle(title)
                .addButton(context.getString(negRes), RockButtonAttr.BUTTON_NEGATIVE, new RockCallBack() {
                    @Override
                    public void onClick(RockDialog rockDialog, View view) {
                        if (listener != null) {
                            listener.onDataResult(new Object[]{DialogInterface.BUTTON_NEGATIVE});
                        }
                        rockDialog.dismiss();
                    }
                })
                .addButton(context.getString(posRes), RockButtonAttr.BUTTON_POSITIVE, new RockCallBack() {
                    @Override
                    public void onClick(RockDialog rockDialog, View view) {
                        if (listener != null) {
                            Object[] res = new Object[4];
                            res[0] = DialogInterface.BUTTON_POSITIVE;
                            CharSequence text = editText.getText();
                            if (text != null && text.length() > 0) {
                                res[1] = text.toString();
                            } else {
                                res[1] = null;
                            }
                            res[2] = date;
                            res[3] = tv_operator.getText().toString();
                            listener.onDataResult(res);
                        }
                        rockDialog.dismiss();
                    }
                })
                .withCustomView(customView)
                .create().show();
    }
}


